<template>
  <MainBox :data="form" :loading="dataInfo.loading" :loadingMsg="dataInfo.loadingMsg">
    <ElFormBox :data="form" :btns="[
      {
        title: '重置',
        icon: 'Refresh',
        type: '',
        click: (form: any, resetFields: Function) => {
          resetFields()
        },
      },
      {
        title: '打印单据',
        icon: 'Printer',
        type: 'primary',
        isRules: true,
        disabled: () => {
          return !!!form.xieYiShuHao
        },
        click: (form: any) => {
          saveBtn(form, 1)
        },
      },
      {
        title: '下载单据',
        icon: 'Download',
        type: 'primary',
        isRules: true,
        disabled: () => {
          return !!!form.xieYiShuHao
        },
        click: (form: any) => {
          saveBtn(form, 3)
        },
      },
      {
        title: '模版参数',
        icon: 'Operation',
        type: 'primary',
        options: options.templateParameters,
      },
    ]">
      <CellBox title="基本信息" isTop>
        <ElFormItemBox label="模版类型" prop="">
          <ElRadioBox :data="form" dataName="type" :options="options.type" />
        </ElFormItemBox>
        <ElFormItemBox label="生产单号" prop="produceTaskBillNo" isRules>
          <ElInputBox :data="form" dataName="produceTaskBillNo" @changeBtn="searchBtn" />
          <template #btn>
            <el-button @click="searchBtn">搜索</el-button>
          </template>
        </ElFormItemBox>
      </CellBox>
      <template v-if="form.xieYiShuHao">
        <CellBox title="产品信息" isTop>
          <FlexBox>
            <ElFormItemBox label="客户名称" prop="customerName">
              <ElInputBox :data="form" dataName="customerName" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="协议书号" prop="xieYiShuHao">
              <ElInputBox :data="form" dataName="xieYiShuHao" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="订单单号" prop="saleOrderGroupNumber">
              <ElInputBox :data="form" dataName="saleOrderGroupNumber" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="产品名称" prop="productName">
              <ElInputBox :data="form" dataName="productName" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="产品编码" prop="productNo">
              <ElInputBox :data="form" dataName="productNo" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="产品单位" prop="productUnit">
              <ElInputBox :data="form" dataName="productUnit" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="标签尺寸" prop="productSize">
              <ElInputBox :data="form" dataName="productSize" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="下单数量" prop="productCount" isRules>
              <ElInputBox :data="form" dataName="productCount" @changeBtn="getPackageDescription" />
            </ElFormItemBox>
          </FlexBox>
        </CellBox>
        <PackageInfo :data="form" isEdit isPrint isTop @changeBtn="getPackageDescription" />
        <CellBox title="其他信息" isTop>
          <FlexBox>
            <ElFormItemBox label="生产日期" prop="productionDate">
              <ElDayBox :data="form" dataName="productionDate" />
            </ElFormItemBox>
            <ElFormItemBox label="包装日期" prop="packageDate">
              <ElDayBox :data="form" dataName="packageDate" />
            </ElFormItemBox>
            <ElFormItemBox label="出标方向" prop="outDirection">
              <ElSelectBox :data="form" dataName="outDirection" :options="proxy.$optionsArr.outDirection" />
            </ElFormItemBox>
            <ElFormItemBox label="贮储期限" prop="shelfLife">
              <ElInputBox :data="form" dataName="shelfLife" />
            </ElFormItemBox>
            <ElFormItemBox label="卷号" prop="volumeNumber">
              <ElInputBox :data="form" dataName="volumeNumber" />
            </ElFormItemBox>
            <ElFormItemBox label="物料编号" prop="goodsCode">
              <ElSelectBox :data="form" dataName="goodsCode" valueName="goodsCode" labelName="goodsName"
                :options="options.goods" />
            </ElFormItemBox>
            <ElFormItemBox label="版本(客户)" prop="customerVersion">
              <ElInputBox :data="form" dataName="customerVersion" />
            </ElFormItemBox>
            <ElFormItemBox label="产品代码(客户)" prop="customerProductNo">
              <ElInputBox :data="form" dataName="customerProductNo" />
            </ElFormItemBox>
            <ElFormItemBox label="备注" prop="boxRemarks" class="wAuto">
              <ElInputBox :data="form" dataName="boxRemarks" type="textarea" />
            </ElFormItemBox>
          </FlexBox>
        </CellBox>
      </template>
    </ElFormBox>
  </MainBox>
</template>
<script setup lang="ts">
const { proxy } = getCurrentInstance() as any;
import './pageCss.scss'; // 引入样式
import { useListComponent } from './pageTs'; // 引入逻辑
const props = withDefaults(
  defineProps<{
    isRadio?: boolean; //单选
    isCheckbox?: boolean; //多选
    maring?: number; //多选
  }>(),
  {
  },
);
//搜索
const searchBtn = () => {
  setLoading()
  proxy.$api.ProductionBusniess.GetProduceTaskBillDetail({
    produceTaskBillNo: form.produceTaskBillNo
  })
    .then((res: any) => {
      form.productionDate = proxy.$day(res.baseData.produceDate || res.baseData.createDate); //生产日期
      form.customerName = res.baseData.customerName; //客户名称
      form.customerCode = res.baseData.customerCode; //客户code
      form.xieYiShuHao = res.baseData.xieYiShuHao; //协议书号
      form.productNo = res.productInfo.baseData.productNumber; //产品编码
      form.productName = res.productInfo.baseData.productName; //产品名称
      form.saleOrderGroupNumber = res.orderBaseData.saleOrderGroupNumber; //订单单号
      form.productSize = res.produceTaskTagList?.map((el: any) => el.productTagSize).join(','); //标签尺寸
      form.productCount = res.orderBaseData?.taoZhuangQuantity; //下单套数
      form.outDirection = res.productInfo?.baseData.outDirection; //出标方向：1上、2下、3左、4右
      form.eachBoxQuantity = res.productInfo?.baseData.eachBoxQuantity; //	包装信息-每箱数量
      form.productCode = res.productInfo.baseData.productCode//产品样式：1平张、2卷式、3折页
      form.productStyle = res.productInfo.baseData.productStyle//产品样式：1平张、2卷式、3折页
      form.productUnit = res.productInfo.baseData.productUnit//产品单位
      form.pageEqualTags = res.productInfo.baseData.pageEqualTags//每张等于X枚数/每卷等于X枚数
      form.packageEqualPages = res.productInfo.baseData.packageEqualPages//每张等于X枚数/每卷等于X枚数
      form.calculationMethod = res.productInfo.baseData.calculationMethod//计算方式
      form.productWeight = res.productInfo.baseData.productWeight//成品重量
      options.goods = res.produceMaterialList.filter((el: any) => el.categoryName === '主料')
      if (options.goods.length === 0) {
        getGood()
      } else {
        form.goodsCode = options.goods[0].goodsCode
      }
      getPackageDescription()
      getInspection()
      dataInfo.loading = false
    })
    .catch(() => {
      form.produceTaskBillNo = ''
      dataInfo.loading = false
    });
}
//获取检验数据
const getInspection = () => {
  proxy.$api.ProductionBusniess.GetPageProductHandInBills({
    pageIndex: 1,
    pageSize: 99,
    queryDataBaseFiled: 'produceTaskBillCode',
    likeWords: form.produceTaskBillNo,
    beginTime: "",
    endTime: "",
    onlySelf: 0,
    isShowDisabled: -1,
    orderByFiled: "",
    orderByType: 0,
    handInType: 0,
    handInBillState: -1,
    inStockState: -1
  })
    .then((res: any) => {
      if (res?.list?.length > 0) {
        const _row = res.list[res.list.length - 1]
        form.packageDate = proxy.$day(_row.createDate)
        form.inspector = _row.createPersonNo
        form.divisioner = _row.createPersonNo
        form.boxRemarks = _row.handInRemark
      }
    })
    .catch(() => {
    });
}
import { packageDescription } from '@/utils/packageDescription.ts';
//获取包装信息
const getPackageDescription = () => {
  const _package = packageDescription(form, form.productCount)
  form.fullBoxQuantity = _package.fullCartonQuantity;// 整箱数量
  form.looseBoxQuantity = _package.bulkQuantity;// 散箱数量
  form.fullBoxSpecification = _package.completePackaging;// 整箱规格
  form.looseBoxSpecification = _package.incompletePackaging;// 整箱规格
}
const saveBtn = (form: any, type: number = 1) => {
  let _form: any = {}
  for (const key in form) {
    _form[capitalizeFirstLetter(key)] = form[key]
  }
  proxy.$DownLoadTemp(
    {
      templateTypeCode: form.type, //	模板类型编号
      customerCode: form.customerCode,
      condition: _form,
    },
    type,
    dataInfo,
  );
  function capitalizeFirstLetter(str) {
    if (typeof str !== 'string' || str.length === 0) return '';
    return str.charAt(0).toUpperCase() + str.slice(1);
  }
}
const getGood = () => {
  // setLoading()
  proxy.$api.Goods.GetMatchPageGoods({
    pageIndex: 1,
    pageSize: 9999,
    queryDataBaseFiled: "",
    likeWords: "",
    isShowDisabled: 0,
    groupCode: "",
    categoryCode: "",
    goodsCode: "",
    unitCode: "",
    isDisable: 0,
    isDelete: 0
  })
    .then((res: any) => {
      options.goods = res.list.filter((el: any) => el.categoryName === '主料')
      dataInfo.loading = false
    })
    .catch(() => {
    });
}
// 组件挂载后执行
onMounted(() => {
});
const { dataInfo, form, setLoading, options } = useListComponent();
</script>
